package it.csi.finregio.finreges.client.services;

import it.crp.crparch.web.struts.service.CRPStrutsService;
import it.crp.crparch.web.struts.service.WebServiceException;
import it.crp.logger.PrdLoggerFactory;
import it.csi.finregio.finreges.client.ServiceLocator;
import it.csi.finregio.finreges.dto.Ente;
import it.csi.finregio.finreges.dto.EnteScheda;
import it.csi.finregio.finreges.dto.Scheda;
import it.csi.finregio.finreges.integration.FinregioService;
import it.csi.finregio.finreges.integration.SchedaService;
import it.csi.finregio.finreges.presentation.form.EntiSchedaContainer;
import it.csi.finregio.finreges.presentation.form.SchedaForm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;

public class SalvaInfoAssEnteService implements CRPStrutsService {

	private Logger log = PrdLoggerFactory.getInstance().getLogger("finreges");

	public Object execute(ActionForm form) throws WebServiceException {
		log.info("[SalvaInfoAssEnteService::execute] BEGIN");
		SchedaForm sf = (SchedaForm) form;
		try {
			SchedaService service = new ServiceLocator().getSchedaService();
			EntiSchedaContainer container = getSchedaForm(form)
					.getTabellaInformazioniAssEnte();
			Collection entiSchedaInseriti = new ArrayList();
			boolean show = getSchedaForm(form).getRequest()
					.getParameter("show") != null;
			Iterator keys = container.getContainerByAssessorato().keySet()
					.iterator();
			while (keys.hasNext()) {
				Ente ente = (Ente) keys.next();
				Map dataMap = (Map) container.getContainerByAssessorato().get(
						ente);
				Collection listaAltriEnti = (Collection) dataMap
						.get(EntiSchedaContainer.ALTRI_ENTI);
				Collection listaStandard = (Collection) dataMap
						.get(EntiSchedaContainer.STANDARD);
				Collection listaNonStandard = (Collection) dataMap
						.get(EntiSchedaContainer.NON_STANDARD);
				updateData(service,listaAltriEnti,(SchedaForm) form);
				updateData(service,listaStandard,(SchedaForm) form);
				updateData(service,listaNonStandard,(SchedaForm) form);

			}
			Scheda scheda = service.findSchedaByIdScheda(sf.getIdScheda());
			boolean changeStato = false;
			if(!sf.getStatoScheda().equals(scheda.getStato().getIdStato())){
				scheda.getStato().setIdStato(sf.getStatoScheda());
				changeStato=true;
				
			}
			if(changeStato){
				service.aggiornaScheda(scheda);
			}
			return container;
		} catch (Exception e) {
			log.error("[SalvaInfoAssEnteService::execute]  " + e.getMessage(),
					e);
			throw new WebServiceException(e.getMessage());
		}
	}

	/**
	 * @param service 
	 * @param schedaForm 
	 * @param listaAltriEnti
	 * @throws Exception 
	 */
	private void updateData(SchedaService service, Collection listaEnti, SchedaForm schedaForm) throws Exception {
		if (listaEnti != null) {
			FinregioService fs = new ServiceLocator().getFinRegioService();
			for (Iterator iterator = listaEnti.iterator(); iterator
					.hasNext();) {
				EnteScheda enteScheda = (EnteScheda) iterator.next();
				Scheda scheda = new Scheda();
				scheda.setIdScheda(schedaForm.getIdScheda());
				enteScheda.setScheda(scheda);
				if (enteScheda.getIdEnteScheda()==null){
					enteScheda = service.createEnteScheda(enteScheda);

				} else {
					enteScheda = service.aggiornaEntiSchede(enteScheda);
				}
			}
		}
	}


	/**
	 * @param form
	 * @return
	 */
	private SchedaForm getSchedaForm(ActionForm form) {
		return ((SchedaForm) form);
	}

}
